package com.tourism.controller;


import com.tourism.domain.dto.auth.AccountLoginDTO;
import com.tourism.domain.entity.User;
import com.tourism.service.AuthService;
import com.tourism.core.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 认证控制器
 *
 * @author lucky
 */
@RequestMapping("/api/auth")
@RestController
public class AuthController {

    @Autowired
    private AuthService authService;

    @PostMapping("login")
    public Result<Object> login(@RequestBody AccountLoginDTO accountLoginDTO) {
        return Result.success(authService.login(accountLoginDTO));
    }

    @GetMapping("/captcha")
    public Result<String> getCaptcha(@RequestParam("randomStr") String randomStr) {
        return Result.success(authService.generateCaptcha(randomStr));
    }

    @GetMapping("info")
    public Result<?> getInfo() {
        return Result.success(authService.getInfo());
    }

    @PostMapping("register")
    public Result<User> register(@RequestBody User user) {
        return authService.register(user) ? Result.success(user) : Result.failure();
    }

    @PostMapping("logout")
    public Result<Boolean> logout(@RequestHeader("authorization") String token) {
        return authService.logout(token) ? Result.success() : Result.failure();
    }
}
